<template>
  <div class="app-container">
    <div class="card-wrap card-padding">
      <div class="table-title-wrap justify-end" v-hasPermi="['maintenance:main:add']">
        <el-button type="primary" size="mini" @click="handleAdd">新增</el-button>
      </div>

      <table-page :loading="loading" :dataList="dataList" border :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @setSizeCurrent="getList">
        <!-- 操作插槽 -->
        <template slot="tableColAc">
          <el-table-column label="操作" width="150" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <e-button size="mini" type="text" @click="handleUpdate(scope.row)" v-hasPermi="['maintenance:main:edit']">编辑
              </e-button>
              <el-button type="text" @click="voids(scope.row)" v-hasPermi="['maintenance:main:void']">作废</el-button>
            </template>
          </el-table-column>
        </template>
        <!-- 普通列插槽 -->
        <template slot="tableCol">
          <el-table-column label="维保服务" align="center" prop="maintenanceServiceName" />
          <el-table-column label="保养公里数(km)" align="center" prop="mileage" />
        </template>
      </table-page>
    </div>

    <!-- 添加或修改测试单表对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body class="form-dialog" width="35%">
      <div style="width: 60%; margin: 0 auto;">
        <el-form ref="form" :model="form" label-width="120px" :rules="rules">
          <el-form-item label="维保项目:" prop="maintenanceServiceId">
            <el-select v-model="form.maintenanceServiceId" clearable style="width: 100%;">
              <template v-for="dict in dict.type.main_tenance_service">
                <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="保养公里数:" prop="mileage">
            <el-input style="width: 100%;" maxlength="13" v-model="form.mileage" clearable @input="form.mileage=form.mileage.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"></el-input>
          </el-form-item>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button @click="cancel">取消</el-button>
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import SearchContent from "@/components/SearchContent/index.vue";
import {
  getMainList,
  mainAdd,
  mainUpdate,
  mainDelete,
  mainDetail,
} from "@/api/globalField";

export default {
  name: 'Main',

  components: {
    SearchContent,
  },
  dicts: ["main_tenance_service"],
  data() {
    return {
      rules: {
        maintenanceServiceId: [
          { required: true, message: "请选择维保项目", trigger: "change" },
        ],
        mileage: [
          { required: true, message: "请输入保养公里数", trigger: "change" },
        ],
      },
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      dataList: [],
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 批量新增操作 */
    bHandleAdd() {
      this.$message.error("批量新增功能暂未开放");
    },
    voids(row) {
      this.$confirm("确认作废?", "作废", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          mainDelete(row.id).then((res) => {
            this.$message({
              type: "success",
              message: "作废成功",
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消作废",
          });
        });
    },
    /** 查询测试单表列表 */
    getList() {
      this.loading = true;
      getMainList(this.queryParams).then((res) => {
        if (res.code == "200") {
          const { rows, total } = res;
          this.dataList = rows;
          this.total = total;
          this.loading = false;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    reset() {
      this.form = {};
    },
    /** 新增操作 */
    handleAdd() {
      this.open = true;
      this.title = "新增";
      this.$refs["form"] && this.$refs["form"].resetFields();
      this.reset();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.title = "编辑";
      mainDetail(row.id).then((res) => {
        this.form = res.data;
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.title == "新增") {
            mainAdd(this.form).then((res) => {
              this.cancel();
              this.getList();
              this.$message.success("新增成功");
            });
          } else {
            // 调取接口
            mainUpdate(this.form).then((res) => {
              this.cancel();
              this.getList();
              this.$message.success("编辑成功");
            });
          }
        }
      });
    },
  },
};
</script>


        <style lang="scss" scoped>
.pagination-container {
  padding: 0 20px 50px !important;
}

.form-dialog {
  ::v-deep .el-dialog__header {
    padding: 10px 20px;
    border-bottom: 1px solid #dcdfe6;

    .el-dialog__headerbtn {
      top: 15px;
    }
  }

  .box-card {
    margin-bottom: 20px;

    .card-title {
      font-size: 16px;
      position: relative;
      margin-left: 8px;
      padding: 0;
    }

    .card-title::after {
      content: "";
      width: 4px;
      height: 18px;
      background-color: aqua;
      display: block;
      position: absolute;
      left: -12px;
      top: 2px;
    }
  }
}
</style>
